package com.ruyuan.eshop.diplomacy.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruyuan.eshop.diplomacy.domain.dto.ProductDetailDTO;
import com.ruyuan.eshop.diplomacy.domain.entity.*;
import com.ruyuan.eshop.diplomacy.mapper.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 *  商品 资源管理
 * @author : zhonghuashishan
 *
 */
@Repository
public class ProductRepository {

    @Resource
    private ItemInfoMapper itemInfoMapper;
    @Resource
    private ItemShelfLifeMapper itemShelfLifeMapper;
    @Resource
    private ProducingAreaMapper producingAreaMapper;
    @Resource
    private SkuBoxSpecsMapper skuBoxSpecsMapper;
    @Resource
    private BrandInfoMapper brandInfoMapper;
    @Resource
    private BuyerInfoMapper buyerInfoMapper;
    @Resource
    private ItemVideoImgMapper itemVideoImgMapper;
    @Resource
    private QualityControlMapper qualityControlMapper;
    @Resource
    private SaleRuleMapper saleRuleMapper;

    /**
     *  查询商品的详情信息
     * @param skuId
     * @return
     */
    public ProductDetailDO queryProductDetail(String skuId){
        // 查询商品的主图信息
        ProductDetailDO productDetailDO = itemInfoMapper.queryProductInfo(skuId);
        // 查询商品的产地信息
        ProducingAreaDO producingArea = getProducingArea(productDetailDO.getProducingAreaId());
        // 商品的重量信息
        SkuBoxSpecsDO skuBoxSpecs = getSkuBoxSpecs(skuId);
        // 商品的品牌信息
        BrandInfoDO brandInfo = getBrandInfo(productDetailDO.getBrandId());
        // 商品的亮点信息
        SaleRuleDO saleRuleDO = getSaleRule(productDetailDO.getItemId());
        // 商品的质检报告
        List<QualityControlDO> qualityControlList = getQualityControlList(skuId);
        // 商品的主图详情信息
        List<ItemVideoImgDO> mainVideoImgList = getItemVideoImgList(productDetailDO.getItemId(),Arrays.asList(1,3,4));
        // 商品的轮播图信息
        List<ItemVideoImgDO> shufflingImgList = getItemVideoImgList(productDetailDO.getItemId(), Arrays.asList(2));
        // 商品的归属买家信息
        BuyerInfoDO buyerInfoDO = getBuyerInfo(String.valueOf(productDetailDO.getCategoryId()));
        // 构建商品的模型
        return buildProductDetailDO(productDetailDO,
                Objects.isNull(producingArea)? null: producingArea.getProducingAreaName(),
                Objects.isNull(skuBoxSpecs) ? null : skuBoxSpecs.getWeightAttr().toPlainString(),
                Objects.isNull(brandInfo) ?null :brandInfo.getBrandName(),
                Objects.isNull(saleRuleDO) ? null : saleRuleDO.getHighlights(),
                qualityControlList,
                mainVideoImgList,
                shufflingImgList,
                buyerInfoDO);
    }

    /**
     * 构建 填充商品详情的信息
     * @param productDetailDO
     * @param producingAreaName
     * @param goodsWeight
     * @param brandName
     * @param highlights
     * @param qualityControlList
     * @param mainVideoImgList
     * @param shufflingImgList
     * @param buyerInfoDO
     * @return
     */
    private ProductDetailDO buildProductDetailDO(ProductDetailDO productDetailDO,
                                      String producingAreaName,
                                      String goodsWeight,
                                      String brandName,
                                      String highlights,
                                      List<QualityControlDO> qualityControlList,
                                      List<ItemVideoImgDO> mainVideoImgList,
                                      List<ItemVideoImgDO> shufflingImgList,
                                      BuyerInfoDO buyerInfoDO){
        productDetailDO.setProducingAreaName(producingAreaName);
        productDetailDO.setGoodsWeight(goodsWeight);
        productDetailDO.setBrand(brandName);
        productDetailDO.setHighlights(highlights);
        productDetailDO.setQualityControlList(qualityControlList);
        productDetailDO.setTeletextList(mainVideoImgList);
        productDetailDO.setVideoImgList(shufflingImgList);
        productDetailDO.setBuyer(buyerInfoDO);

        return productDetailDO;
    }

    /**
     * 获取商品的买手信息
     * @param categoryId
     * @return
     */
    private BuyerInfoDO getBuyerInfo(String categoryId){
        QueryWrapper<BuyerInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CATEGORY_ID", categoryId);

        return buyerInfoMapper.selectOne(queryWrapper);
    }
    /**
     * 查询商品的某种图文信息
     * @param itemId
     * @param contentTypeList
     * @return
     */
    private List<ItemVideoImgDO> getItemVideoImgList(String itemId,List<Integer> contentTypeList){
        QueryWrapper<ItemVideoImgDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ITEM_ID", itemId);
        queryWrapper.in("CONTENT_TYPE", contentTypeList);

        return itemVideoImgMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品的亮点信息
     * @param itemId
     * @return
     */
    private SaleRuleDO getSaleRule(String itemId){
        QueryWrapper<SaleRuleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ITEM_ID", itemId);

        return saleRuleMapper.selectOne(queryWrapper);
    }

    /**
     * 获取商品的质检报告详情
     * @param skuId
     * @return
     */
    private List<QualityControlDO> getQualityControlList(String skuId){
        QueryWrapper<QualityControlDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SKU_ID", skuId);
        return qualityControlMapper.selectList(queryWrapper);
    }
    /**
     * 查询商品的品牌信息
     * @param brandId
     * @return
     */
    private BrandInfoDO getBrandInfo(Integer brandId){
        return brandInfoMapper.selectById(brandId);
    }

    /**
     * 查询sku商品的规格信息
     * @param skuId
     * @return
     */
    private SkuBoxSpecsDO getSkuBoxSpecs(String skuId){
        QueryWrapper<SkuBoxSpecsDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SKU_ID", skuId);

        return skuBoxSpecsMapper.selectOne(queryWrapper);
    }

    /**
     * 查询产地信息
     * @param producingAreaId
     * @return
     */
    private ProducingAreaDO getProducingArea(Integer producingAreaId){
        return producingAreaMapper.selectById(producingAreaId);
    }

    /**
     * 查询保质期信息
     * @param itemId
     * @return
     */
    private ItemShelfLifeDO getItemShelfLife(String itemId){
        QueryWrapper<ItemShelfLifeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ITEM_ID", itemId);

      return itemShelfLifeMapper.selectOne(queryWrapper);
    }

}
